/**
 * Created by zhanglihe on 15/11/10.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'data_report',
                condition: {limit: 10, offset: 0, level: 255},
                model: {
                    event: {
                        '.show_sql:click': 'showSql'
                    },
                    showSql: function (e) {
                        e.preventDefault();
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title: '数据Sql',
                            content: this.data.sql,
                            width: 600,
                            ok: function () {
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();


                    }

                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            var _this = this;
            //日期
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm'
                });
            });

            //查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                var start_time = $('#start_time').val();
                var end_time = $('#end_time').val();
                var type = $("select[name='x'] option:selected").val();
                var ids = new Array();
                var source = 0;
                $("input[name='data_report']:checked").each(function () {
                    ids.push($(this).val());
                })
                ids = ids.join(',');
                if (ids == '' || ids == undefined) {
                    Alert({msg: '请选择要查看的项', type: 'error'});
                    return false;
                }

                //alert(startTime);return false;
                if (start_time == '' || start_time == 'undefined') {
                    Alert({msg: '请填写查询开始时间', type: 'error'});
                    return false;
                }
                if (end_time == '' || end_time == 'undefined') {
                    Alert({msg: '请填写查询结束时间', type: 'error'});
                    return false;
                }
                location.href = '/admin/see_data/' + ids + '/' + type + '/' + start_time + '/' + end_time + '/' + source;
            })

            //处理不能按小时数据查询
            $("select[name='x']").change(function (e) {
                _this.collection.condition.type = $("select[name='x'] option:selected").val();
                _this.collection.reload();
            })
            $('.quick_time').on('click', function (e) {
                e.preventDefault();
                var times = $(this).attr('rel');
                var start_time, end_time;
                var curDate = new Date();
                if (times == 72) {
                    start_time = _this.formatTime(new Date(curDate.getTime() - 72 * 60 * 60 * 1000));
                } else if (times == 24) {
                    start_time = _this.formatTime(new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate(), 0, 0, 0));
                }
                end_time = _this.formatTime(new Date());
                $('#start_time').val(start_time);
                $('#end_time').val(end_time);
            });


            /**
             * 按状态查询
             */
            //$('#btn-status').find('a').on('click', function (e) {
            //    e.preventDefault();
            //    var ids = new Array();
            //    $("input[class='data_report']:checked").each(function () {
            //        ids.push($(this).val())
            //    });
            //    var ids_set = ids.join(',');
            //    if (ids_set == '' || ids_set == undefined) {
            //        Alert({msg: '请选择要查看的项', type: 'error'});
            //        return false;
            //    }
            //    var type = $(this).attr('rel');
            //    if (ids.length > 1) {
            //        location.href = '/admin/see_data/' + ids_set + '/' + type
            //    } else {
            //        location.href = '/admin/signal/' + ids_set + '/' + type
            //    }
            //
            //    $(e.target).addClass('active').siblings().removeClass('active');
            //});
            //$("#btn-check_all").on("click", function (e) {
            //    e.preventDefault();
            //    $("#data-list :checkbox").prop("checked", !$("#data-list :checkbox").prop("checked"));
            //});

        },
        formatTime: function (date) {
            var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
            var curDay = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
            return date.getFullYear() + '-' + month + '-' + curDay + ' ' + hours + ':' + minutes + ':' + seconds;
        }

    };

    $(function () {
        new index();
    });
});